﻿namespace Deppton.Mobile.Views
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Forms;
    using Deppton.Model;

    public partial class OptionsQuestionForm : Deppton.Mobile.Views.QuestionForm
    {
        public OptionsQuestionForm()
        {
            InitializeComponent();

            var options = new BindingList<QuestionOption>();
            this.Options = options;
            this.OptionControls = new Dictionary<QuestionOption, RadioButton>();

            options.AddingNew += new AddingNewEventHandler(this.OptionsAddingNew);
            options.ListChanged += new ListChangedEventHandler(this.OptionsChanged);
        }

        public ICollection<QuestionOption> Options
        {
            get;
            private set;
        }

        public override bool ControlsEnabled
        {
            get
            {
                return base.ControlsEnabled;
            }
            set
            {
                base.ControlsEnabled = value;

                foreach (var control in this.OptionControls)
                {
                    control.Value.Enabled = value;
                }
            }
        }

        public override string Value
        {
            get
            {
                return this.OptionControls.Where(o => o.Value.Checked).Select(o => o.Key.Key).SingleOrDefault();
            }

            set
            {
                var control = this.OptionControls.Where(o => o.Key.Key == value).Select(o => o.Value).SingleOrDefault();

                if (control != null)
                {
                    control.Checked = true;
                }
                else
                {
                    foreach (var optionControl in this.OptionControls)
                    {
                        optionControl.Value.Checked = false;
                    }
                }
            }
        }

        private Dictionary<QuestionOption, RadioButton> OptionControls
        {
            get;
            set;
        }

        private void OptionsAddingNew(object sender, AddingNewEventArgs e)
        {
            this.RefreshInformation();
        }

        private void OptionsChanged(object sender, ListChangedEventArgs e)
        {
            this.RefreshInformation();
        }

        private void RefreshInformation()
        {
            foreach (var option in new List<QuestionOption>(this.OptionControls.Keys))
            {
                var control = this.OptionControls[option];

                this.RadioButtonsOptionsPanel.Controls.Remove(control);

                this.OptionControls.Remove(option);
            }

            int nextLocation = 3;
            foreach (var option in this.Options)
            {
                var control = new RadioButton
                {
                    Location = new System.Drawing.Point(3, nextLocation),
                    Size = new System.Drawing.Size(this.RadioButtonsOptionsPanel.Width - 6, 20),
                    Text = option.Description,
                    Tag = option,
                    Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
                    Enabled = this.ControlsEnabled
                };

                this.OptionControls.Add(option, control);
                this.RadioButtonsOptionsPanel.Controls.Add(control);
                nextLocation += 23;
            }
        }
    }
}

